import { defineConfig, loadEnv, type ConfigEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import tailwindcss from '@tailwindcss/vite'
import path from 'path'


// https://vite.dev/config/
const viteConfig = defineConfig((mode: ConfigEnv) => {
  const env = loadEnv(mode.mode, process.cwd());
  return {
    plugins: [
      vue(),
      tailwindcss(),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
      }
    },
    server: {
      hmr: true,
      host: '0.0.0.0',
      open: true,
      proxy: {
        '/es': {
          target: env.VITE_BASE_URL, // 实际后端地址
          changeOrigin: true,
          ws: true,
          rewrite: (path) => path.replace(/^\/es/, ''), // 路径重写
          bypass(req: any, res: any, options: any) {
            const proxyUrl = new URL(options.rewrite(req.url), (options.target) as string)?.href
            res.setHeader("x-res-proxyUrl", proxyUrl);
          },
          // secure: false， // 不校验ssl证书
        },
      },
    }
  }
  
})
export default viteConfig;